/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import controladores.MaterialJpaControllerExt;

import entidades.Material;
import entidades.SubTipoMaterial;
import entidades.TipoMaterial;
import entidades.UnidadMedida;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import reportes.GenReportes;
import rn.MaterialRN;

/**
 *
 * @author Pablo
 */
public class FormMateriales extends javax.swing.JFrame {

    private Material material, material_seleccionado;
    DefaultTableModel modelo_de_tabla;
    private int opcion_modificar = 0;

    public FormMateriales() {
        initComponents();

        material = new Material();

        bt_modificar.setEnabled(false);
        bt_eliminar.setEnabled(false);
        
        tf_descripcion.setDocument(new JTextFieldToUpperCase());
        tf_codigo.setDocument(new JTextFieldToUpperCase());
        tf_codigo_barras.setDocument(new JTextFieldToUpperCase());
        
        tf_descripcion.getDocument().putProperty("campoDelEvento", "descripcion");
        tf_descripcion.getDocument().addDocumentListener(new TexChangeListener());  
        tf_descripcion.addKeyListener(new KeyPressedClass());
        tf_codigo.getDocument().putProperty("campoDelEvento", "codigo");
        tf_codigo.getDocument().addDocumentListener(new TexChangeListener());
        tf_codigo.addKeyListener(new KeyPressedClass());        
        tf_codigo_barras.getDocument().putProperty("campoDelEvento", "codigoBarras");
        tf_codigo_barras.getDocument().addDocumentListener(new TexChangeListener());
        tf_codigo_barras.addKeyListener(new KeyPressedClass());
        
        tabla_conincidencias.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
        tabla_conincidencias.getActionMap().put("Enter", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.println("Se presiono la tecla enter");
                int fila = tabla_conincidencias.getSelectedRow();
                String s_id = (String) tabla_conincidencias.getValueAt(fila, 0);
                material_seleccionado = MaterialRN.getMaterialXId(new Long(s_id));
                llenarCampos(material_seleccionado);
                habilitarCampos(false);
                bt_grabar.setEnabled(false);
                bt_modificar_stock.setEnabled(false);
                bt_modificar.setEnabled(true);
                bt_eliminar.setEnabled(true);
                tabla_conincidencias.requestFocus();
                tabla_conincidencias.changeSelection(fila, 0, false, false);
            }
        });
        
        llenarCombos();
        this.setLocationRelativeTo(null);
        tf_codigo.requestFocus();

        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    
    @SuppressWarnings("unchecked")
    
    public boolean verificarDatosDuplicados(Material v, Long id_excepcion) {
        boolean b = true;
        if (MaterialRN.yaExiste(v.getDescripcion(), MaterialJpaControllerExt.DESCRIPCION, id_excepcion)) {
            b = false;
            mostrarMensaje("Ya existe un material con la misma 'descripción'");
        }
        if (MaterialRN.yaExiste(v.getCodigo(), MaterialJpaControllerExt.CODIGO, id_excepcion)) {
            b = false;
            mostrarMensaje("Ya existe un material con el mismo 'codigo'");
        }
        if (MaterialRN.yaExiste(v.getCodigo_barras(), MaterialJpaControllerExt.COD_BARRAS, id_excepcion)) {
            b = false;
            mostrarMensaje("Ya existe un material con el mismo 'codigo de barras'");
        }
        return b;
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        tf_combustible_actual1 = new javax.swing.JTextField();
        bt_ingresar1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tf_combustible_actual4 = new javax.swing.JTextField();
        bt_cancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        tf_codigo = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        tf_descripcion = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        combo_tipo_material = new javax.swing.JComboBox();
        bt_tipo = new javax.swing.JButton();
        bt_grabar = new javax.swing.JButton();
        bt_limpiar = new javax.swing.JButton();
        bt_modificar = new javax.swing.JButton();
        bt_eliminar = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        tf_codigo_barras = new javax.swing.JTextField();
        bt_modificar_stock = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        combo_sub_tipo = new javax.swing.JComboBox();
        bt_sub_tipo = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        combo_unidad_medida = new javax.swing.JComboBox();
        bt_unidad_medida = new javax.swing.JButton();
        tf_precio = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        tf_stock = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        tf_stock_minimo = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tabla_conincidencias = new javax.swing.JTable();
        bt_imprimir = new javax.swing.JButton();
        bt_cancelar3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        combo_listados = new javax.swing.JComboBox();
        tx_categoria = new javax.swing.JLabel();
        combo_subtipo_filtro = new javax.swing.JComboBox();
        tx_marca = new javax.swing.JLabel();
        combo_tipo_material_filtro = new javax.swing.JComboBox();
        tx_marca1 = new javax.swing.JLabel();
        combo_unidades_filtro = new javax.swing.JComboBox();

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("Combustible Actual (M3):");

        tf_combustible_actual1.setEnabled(false);

        bt_ingresar1.setText("INGRESAR");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("Ingreso (M3):");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Ingreso (M3):");

        tf_combustible_actual4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar.setText("CANCELAR");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(0, 0, 153));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("ARCHIVO DE MATERIALES");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tf_codigo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Código:");

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setText("Descripción:");

        tf_descripcion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel14.setText("Tipo Material:");

        combo_tipo_material.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_tipo_material.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_tipo_materialActionPerformed(evt);
            }
        });

        bt_tipo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png"))); // NOI18N
        bt_tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_tipoActionPerformed(evt);
            }
        });

        bt_grabar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_grabar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_grabar.setText("GRABAR");
        bt_grabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_grabarActionPerformed(evt);
            }
        });

        bt_limpiar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_limpiar.setText("LIMPIAR");
        bt_limpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_limpiarActionPerformed(evt);
            }
        });

        bt_modificar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_modificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil.png"))); // NOI18N
        bt_modificar.setText("MODIFICAR");
        bt_modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modificarActionPerformed(evt);
            }
        });

        bt_eliminar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_eliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        bt_eliminar.setText("ELIMIMAR");
        bt_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_eliminarActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Código de barras:");

        tf_codigo_barras.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        bt_modificar_stock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/application_edit.png"))); // NOI18N
        bt_modificar_stock.setText("MODIFICAR STOCK");
        bt_modificar_stock.setEnabled(false);
        bt_modificar_stock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modificar_stockActionPerformed(evt);
            }
        });

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("Sub tipo:");

        combo_sub_tipo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_sub_tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_sub_tipoActionPerformed(evt);
            }
        });

        bt_sub_tipo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png"))); // NOI18N
        bt_sub_tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_sub_tipoActionPerformed(evt);
            }
        });

        jLabel18.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel18.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel18.setText("Unidad de Medida:");

        combo_unidad_medida.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_unidad_medida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_unidad_medidaActionPerformed(evt);
            }
        });

        bt_unidad_medida.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png"))); // NOI18N
        bt_unidad_medida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_unidad_medidaActionPerformed(evt);
            }
        });

        tf_precio.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel15.setText("Precio Unitario:");

        tf_stock.setEditable(false);
        tf_stock.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tf_stock.setText("0");
        tf_stock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_stockActionPerformed(evt);
            }
        });

        jLabel17.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel17.setText("Stock:");

        tf_stock_minimo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel19.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel19.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel19.setText("Stock Mínimo:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(bt_eliminar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_modificar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_limpiar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_grabar))
                            .addComponent(bt_modificar_stock, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(132, 132, 132)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel14))))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_codigo_barras)
                            .addComponent(tf_descripcion)
                            .addComponent(tf_codigo, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(combo_tipo_material, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(bt_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(combo_sub_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(bt_sub_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel18, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel15, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel19, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_stock_minimo)
                            .addComponent(tf_stock)
                            .addComponent(tf_precio, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(combo_unidad_medida, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_unidad_medida, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_codigo_barras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel14)
                        .addComponent(combo_tipo_material, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bt_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel16)
                        .addComponent(combo_sub_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bt_sub_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel18)
                        .addComponent(combo_unidad_medida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bt_unidad_medida, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_precio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_stock_minimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19))
                .addGap(23, 23, 23)
                .addComponent(bt_modificar_stock)
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_grabar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_limpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_modificar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_eliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        tabla_conincidencias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Documento", "Nombre"
            }
        ));
        jScrollPane4.setViewportView(tabla_conincidencias);

        bt_imprimir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_imprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/printer.png"))); // NOI18N
        bt_imprimir.setText("IMPRIMIR LISTA");
        bt_imprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_imprimirActionPerformed(evt);
            }
        });

        bt_cancelar3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_excel.png"))); // NOI18N
        bt_cancelar3.setText("EXPORTAR A EXCEL");

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Listar"));

        combo_listados.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        combo_listados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar...", "Todos", "Tipo de Material", "SubTipo", "Unidades de Medida" }));
        combo_listados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_listadosActionPerformed(evt);
            }
        });

        tx_categoria.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_categoria.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_categoria.setText("Subtipo:");
        tx_categoria.setEnabled(false);

        combo_subtipo_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_subtipo_filtro.setEnabled(false);
        combo_subtipo_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_subtipo_filtroActionPerformed(evt);
            }
        });

        tx_marca.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_marca.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_marca.setText("Tipo de Material:");
        tx_marca.setEnabled(false);

        combo_tipo_material_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_tipo_material_filtro.setEnabled(false);
        combo_tipo_material_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_tipo_material_filtroActionPerformed(evt);
            }
        });

        tx_marca1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_marca1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_marca1.setText("Unidades:");
        tx_marca1.setEnabled(false);

        combo_unidades_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_unidades_filtro.setEnabled(false);
        combo_unidades_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_unidades_filtroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(combo_listados, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)
                        .addComponent(tx_marca))
                    .addComponent(tx_marca1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(combo_unidades_filtro, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(combo_tipo_material_filtro, 0, 130, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(tx_categoria)
                .addGap(7, 7, 7)
                .addComponent(combo_subtipo_filtro, 0, 129, Short.MAX_VALUE)
                .addGap(82, 82, 82))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(combo_listados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo_tipo_material_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tx_marca))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo_subtipo_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tx_categoria)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(combo_unidades_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tx_marca1))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 767, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(719, 719, 719)
                        .addComponent(bt_cancelar3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_imprimir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_cancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bt_cancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                    .addComponent(bt_imprimir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bt_cancelar3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void combo_subtipo_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_subtipo_filtroActionPerformed
        List<Material> resultado = new ArrayList();
        SubTipoMaterial item_selected = (SubTipoMaterial) combo_subtipo_filtro.getSelectedItem();
        resultado = MaterialRN.getMaterialesPor(item_selected.toString(), "SubTipoMaterial");
        llenarTablaResultado(resultado);
    }//GEN-LAST:event_combo_subtipo_filtroActionPerformed

    private void bt_grabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_grabarActionPerformed
        if (chaquearCamposRequeridos()) {
            if (verificarValoresDecimales()) {
                setearMaterial();
                Long id_excep = null;
                if (opcion_modificar == 1) {
                    id_excep = material_seleccionado.getId();
                }
                if (verificarDatosDuplicados(material, id_excep)) {
                    if (opcion_modificar == 1) {
                        material.setId(material_seleccionado.getId());
                        MaterialRN.modificar(material);
                        mostrarMensaje("Datos Modificados!!!");
                        opcion_modificar = 0;
                    } else {
                        material.setStock(0); //Se inicia con stock 0.
                        MaterialRN.create(material);
                        mostrarMensaje("Registro Guardado!!!");
                        opcion_modificar = 0;
                    }
                    limpiarCampos();
                    material = new Material();
                    bt_modificar.setEnabled(false);
                    bt_modificar_stock.setEnabled(false);
                    tf_codigo.requestFocus();
                }
                material_seleccionado = null;
            }
            mostrarMensaje("El valor ingresado es invalido");
        } else {
            mostrarMensaje("Campos requeridos no pueden estar vacios!!!");
            tf_codigo.requestFocus();
        }
    }//GEN-LAST:event_bt_grabarActionPerformed

    private void bt_limpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_limpiarActionPerformed
        limpiarCampos();
        iniciarModeloTabla();
        tabla_conincidencias.setModel(modelo_de_tabla);
        habilitarCampos(true);
        bt_grabar.setEnabled(true);
        bt_modificar.setEnabled(false);
        bt_modificar_stock.setEnabled(false);
        bt_eliminar.setEnabled(false);
        opcion_modificar = 0;
        material_seleccionado = null;
        tf_codigo.requestFocus();
    }//GEN-LAST:event_bt_limpiarActionPerformed

    private void bt_modificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_modificarActionPerformed
        habilitarCampos(true);
        bt_grabar.setEnabled(true);
        bt_modificar_stock.setEnabled(true);
        opcion_modificar = 1;        
    }//GEN-LAST:event_bt_modificarActionPerformed

    private void bt_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_eliminarActionPerformed
        if (material_seleccionado != null) {
            int c = JOptionPane.showConfirmDialog(null, "¿Está seguro de eliminar el material?");
            System.out.println(c);
            if (c == 0) {
                MaterialRN.eliminarMaterial(material_seleccionado.getId());
                mostrarMensaje("Material eliminado!!!");
                limpiarCampos();
                habilitarCampos(true);
                bt_grabar.setEnabled(true);
                bt_modificar.setEnabled(false);
                bt_modificar_stock.setEnabled(false);
                bt_eliminar.setEnabled(false);
                material_seleccionado = null;
                tf_codigo.requestFocus();
            }
        }
        else
            mostrarMensaje("Debe seleccionar un material");
    }//GEN-LAST:event_bt_eliminarActionPerformed

    private void combo_tipo_materialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_tipo_materialActionPerformed
        if (combo_tipo_material.getSelectedItem() != null) {
            TipoMaterial item_selected = (TipoMaterial) combo_tipo_material.getSelectedItem();
            List<SubTipoMaterial> subTipoMaterialesXMaterial = MaterialRN.getSubTipoMaterialPorTipoMaterial(item_selected.getId());
            combo_sub_tipo.removeAllItems();
            for (SubTipoMaterial m : subTipoMaterialesXMaterial) {
                combo_sub_tipo.addItem(m);
            }
        }
    }//GEN-LAST:event_combo_tipo_materialActionPerformed

    private void combo_listadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_listadosActionPerformed
        List<Material> resultado = new ArrayList();
        String item_selected = (String) combo_listados.getSelectedItem();
        if (!item_selected.equals("Seleccionar...")) {
            switch (item_selected) {
                case "Todos":
                    habilitarFiltro(false);
                    resultado = MaterialRN.getTodosMateriales();
                    break;
                case "Tipo de Material":
                    habilitarFiltro(false);
                    combo_tipo_material_filtro.setEnabled(true);                    
                    break;
                case "SubTipo":
                    habilitarFiltro(false);                    
                    combo_subtipo_filtro.setEnabled(true);
                    resultado = MaterialRN.getMaterialesPor(item_selected, "SubTipoMaterial");
                    break;
                case "Unidades de Medida":
                    habilitarFiltro(false);                    
                    combo_unidades_filtro.setEnabled(true);
                    resultado = MaterialRN.getMaterialesPor(item_selected, "UnidadMedida");
                    break;    
            }
            llenarTablaResultado(resultado);
        
        }
    }//GEN-LAST:event_combo_listadosActionPerformed

    private void combo_tipo_material_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_tipo_material_filtroActionPerformed
        List<Material> resultado = new ArrayList();
        TipoMaterial item_selected = (TipoMaterial) combo_tipo_material_filtro.getSelectedItem();
        resultado = MaterialRN.getMaterialesPor(item_selected.getDescripcion(), "TipoMaterial");
        llenarTablaResultado(resultado);
    }//GEN-LAST:event_combo_tipo_material_filtroActionPerformed

    private void bt_imprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_imprimirActionPerformed

    }//GEN-LAST:event_bt_imprimirActionPerformed

    private void bt_tipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_tipoActionPerformed
       new ABM_TipoMaterial(this);
    }//GEN-LAST:event_bt_tipoActionPerformed

    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_cancelarActionPerformed

    private void bt_modificar_stockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_modificar_stockActionPerformed
        //new FormIngresoRepuestos(material_seleccionado);
    }//GEN-LAST:event_bt_modificar_stockActionPerformed

    private void combo_sub_tipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_sub_tipoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_combo_sub_tipoActionPerformed

    private void bt_sub_tipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_sub_tipoActionPerformed
        new ABM_SubTipoMateriales(this, (TipoMaterial)combo_tipo_material.getSelectedItem());
    }//GEN-LAST:event_bt_sub_tipoActionPerformed

    private void combo_unidad_medidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_unidad_medidaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_combo_unidad_medidaActionPerformed

    private void bt_unidad_medidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_unidad_medidaActionPerformed
        new ABM_UnidadMedida(this);
    }//GEN-LAST:event_bt_unidad_medidaActionPerformed

    private void combo_unidades_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_unidades_filtroActionPerformed
        List<Material> resultado = new ArrayList();
        UnidadMedida item_selected = (UnidadMedida) combo_unidades_filtro.getSelectedItem();
        resultado = MaterialRN.getMaterialesPor(item_selected.getDescripcion(), "UnidadMedida");
        llenarTablaResultado(resultado);
    }//GEN-LAST:event_combo_unidades_filtroActionPerformed

    private void tf_stockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_stockActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_stockActionPerformed

    private void habilitarFiltro(boolean b) {
        combo_unidades_filtro.setEnabled(b);
        combo_subtipo_filtro.setEnabled(b);        
        combo_tipo_material_filtro.setEnabled(b);
    }
    
    private void setearMaterial() {
        //Fields
        material.setDescripcion(tf_descripcion.getText());
        material.setCodigo(tf_codigo.getText());
        material.setCodigo_barras(tf_codigo_barras.getText());        
        material.setStock_minimo(Double.parseDouble(tf_stock_minimo.getText()));
        material.setPrecio_unitario(Double.parseDouble(tf_precio.getText()));        
        
        //Combos
        material.setSubTipoMaterial((SubTipoMaterial) combo_sub_tipo.getSelectedItem());
        material.setUnidadMedida((UnidadMedida) combo_unidad_medida.getSelectedItem());        
    }
    
    private boolean verificarValor(String text) {       
        return (text.matches("[+]?\\d*(\\.\\d+)?") && text.equals("")==false);
    }
    
    private boolean verificarValoresDecimales() {
        List<JTextField> campos = new ArrayList<>();
        campos.add(tf_precio);
        campos.add(tf_stock_minimo);
        campos.add(tf_stock);
        
        blanquearCampos();
        for(JTextField t: campos){
            if(!verificarValor(t.getText())){
                t.setBackground(Color.PINK);
                return false;
            }
        }
        return true;        
    }
    
    private void blanquearCampos(){
        tf_precio.setBackground(Color.WHITE);
        tf_stock.setBackground(Color.WHITE);
        tf_stock_minimo.setBackground(Color.WHITE);
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JButton bt_cancelar3;
    private javax.swing.JButton bt_eliminar;
    private javax.swing.JButton bt_grabar;
    private javax.swing.JButton bt_imprimir;
    private javax.swing.JButton bt_ingresar1;
    private javax.swing.JButton bt_limpiar;
    private javax.swing.JButton bt_modificar;
    private javax.swing.JButton bt_modificar_stock;
    private javax.swing.JButton bt_sub_tipo;
    private javax.swing.JButton bt_tipo;
    private javax.swing.JButton bt_unidad_medida;
    private javax.swing.JComboBox combo_listados;
    public javax.swing.JComboBox combo_sub_tipo;
    private javax.swing.JComboBox combo_subtipo_filtro;
    public javax.swing.JComboBox combo_tipo_material;
    private javax.swing.JComboBox combo_tipo_material_filtro;
    public javax.swing.JComboBox combo_unidad_medida;
    private javax.swing.JComboBox combo_unidades_filtro;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tabla_conincidencias;
    private javax.swing.JTextField tf_codigo;
    private javax.swing.JTextField tf_codigo_barras;
    private javax.swing.JTextField tf_combustible_actual1;
    private javax.swing.JTextField tf_combustible_actual4;
    private javax.swing.JTextField tf_descripcion;
    private javax.swing.JTextField tf_precio;
    private javax.swing.JTextField tf_stock;
    private javax.swing.JTextField tf_stock_minimo;
    private javax.swing.JLabel tx_categoria;
    private javax.swing.JLabel tx_marca;
    private javax.swing.JLabel tx_marca1;
    // End of variables declaration//GEN-END:variables
    
    private boolean chaquearCamposRequeridos() {

        ArrayList<JTextField> campos_requeridos = new ArrayList();
        campos_requeridos.add(tf_descripcion);
        campos_requeridos.add(tf_codigo);
        
        for (JTextField t : campos_requeridos) {
            if (t.getText().startsWith(" ") || t.getText().isEmpty()) {
                return false;
            }
        }

        return true;
    }

    private void limpiarCampos() {
        tf_descripcion.setText(null);        
        tf_codigo.setText(null);
        tf_codigo_barras.setText(null);
        tf_precio.setText(null);
        tf_stock.setText(null);
        tf_stock_minimo.setText(null);        
    }

    private void mostrarMensaje(String mensaje) {
        JOptionPane.showMessageDialog(null, mensaje);
    }

    public void llenarCombos() {
        List<TipoMaterial> todosTipoMateriales = MaterialRN.getTodosTipoMateriales();
        List<SubTipoMaterial> todosSubTipoMateriaes = MaterialRN.getTodosSubTipoMateriaes();
        List<UnidadMedida> todosUnidadMedidaes = MaterialRN.getTodosUnidadMedidaes();
        
        llenarCombo(combo_tipo_material, todosTipoMateriales);
        llenarCombo(combo_sub_tipo, todosSubTipoMateriaes);
        llenarCombo(combo_unidad_medida, todosUnidadMedidaes);        
        llenarCombo(combo_tipo_material_filtro, todosTipoMateriales);
        llenarCombo(combo_subtipo_filtro, todosSubTipoMateriaes);
        llenarCombo(combo_unidades_filtro, todosUnidadMedidaes);        
    }
    

    public void llenarCombo(JComboBox combo, List lista) {
        for (Object c : lista) {
            combo.addItem(c);
        }
    }

    private void llenarTablaResultado(List<Material> resultado) {
        iniciarModeloTabla();
        for (Material c : resultado) {

            Object fila[] = new Object[]{
                c.getCodigo(), c.getCodigo_barras(), c.getDescripcion(), c.getSubTipoMaterial().getTipoMaterial().getDescripcion(), c.getSubTipoMaterial()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla_conincidencias.setModel(modelo_de_tabla);
        iniciarModeloColumna();

    }    

    public class TexChangeListener implements DocumentListener {

        public void insertUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }

        public void removeUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }

        public void changedUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }
    }

    private void iniciarModeloTabla() {
        Object datos[][] = {};
        String[] columnNames = {"Codigo", "Barras" ,"Descripcion", "Tipo Material", "SubTipo Material"};
        modelo_de_tabla = new DefaultTableModel(datos, columnNames);
    }

    private void iniciarModeloColumna() {
        TableColumnModel columnModel = tabla_conincidencias.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth(10);
        columnModel.getColumn(2).setPreferredWidth(20);
    }

    public void LlenarTablaConicidencias(javax.swing.event.DocumentEvent evt) {

        String campo_del_evento = (String) evt.getDocument().getProperty("campoDelEvento");
        List<Material> listaPersonas = new ArrayList<>();

        if (campo_del_evento.equals("descripcion")) {
            listaPersonas = MaterialRN.getMaterialesLike(tf_descripcion.getText(), MaterialJpaControllerExt.DESCRIPCION);
        }
        if (campo_del_evento.equals("codigo")) {
            listaPersonas = MaterialRN.getMaterialesLike(tf_codigo.getText(), MaterialJpaControllerExt.CODIGO);
        }
        if (campo_del_evento.equals("codigoBarras")) {
            listaPersonas = MaterialRN.getMaterialesLike(tf_codigo_barras.getText(), MaterialJpaControllerExt.COD_BARRAS);
        }        
        
        iniciarModeloTabla();

        for (Material c : listaPersonas) {

            Object fila[] = new Object[]{
                c.getCodigo(), c.getCodigo_barras(), c.getDescripcion(), c.getSubTipoMaterial().getTipoMaterial().getDescripcion(), c.getSubTipoMaterial().getDescripcion()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla_conincidencias.setModel(modelo_de_tabla);
        iniciarModeloColumna();
    }

    public class KeyPressedClass implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            accion(e);
        }

        public void accion(KeyEvent e) {
            if (e.getSource() == tf_descripcion || e.getSource() == tf_codigo) {
                if (e.VK_DOWN == e.getKeyCode()) {
                    tabla_conincidencias.requestFocus();
                    tabla_conincidencias.changeSelection(0, 0, false, false);
                }
            }
        }
    }

    private void llenarCampos(Material resul) {
        //Fields
        tf_descripcion.setText(resul.getDescripcion());        
        tf_codigo.setText(resul.getCodigo());
        tf_codigo_barras.setText(resul.getCodigo_barras());
        tf_precio.setText(""+resul.getPrecio_unitario());
        tf_stock.setText(""+resul.getStock());
        tf_stock_minimo.setText(""+resul.getStock_minimo());
        
        //Combos
        combo_unidad_medida.setSelectedItem(resul.getUnidadMedida());
        combo_tipo_material.setSelectedItem(resul.getSubTipoMaterial().getTipoMaterial());
        combo_sub_tipo.setSelectedItem(resul.getSubTipoMaterial());
    }

    private void habilitarCampos(boolean b) {
        tf_descripcion.setEditable(b);        
        tf_codigo.setEditable(b);
        tf_codigo_barras.setEditable(b);
        tf_precio.setEditable(b);        
        tf_stock_minimo.setEditable(b);
        //Combos        
        combo_tipo_material.setEnabled(b);
        combo_sub_tipo.setEnabled(b);
        combo_unidad_medida.setEnabled(b);
    }

    class JTextFieldToUpperCase extends PlainDocument {

        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
            super.insertString(offset, str.toUpperCase(), attr);
        }
    }

}
